package cc.mrbird.assets.controller;

import cc.mrbird.assets.domain.AssetMana;
import cc.mrbird.assets.service.AssetManaService;
import cc.mrbird.assets.utils.FileUtil;
import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.domain.UnitInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Api(description = "资产信息管理接口")
@Controller
public class AssetManaController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AssetManaService assetManaService;
    @Autowired
    private FileUtil fileUtil;
    @Log("获取资产信息")
    @RequestMapping(value = "assetinfo",method = RequestMethod.POST)
    /* @RequiresPermissions("assetinfo:list")*/
    public String index() {
        return "asset/assetinfo/assetinfo";
    }
    @Log("获取资产信息")
    @ApiOperation(value = "获取资产信息" ,  notes="获取资产信息")
    @RequestMapping(value = "assetinfo/list",method = RequestMethod.POST)
    /*@RequiresPermissions("assetinfo:list")*/
    @ResponseBody
    public Map<String, Object> assetinfoList(QueryRequest request, AssetMana assetMana) {
        return super.selectByPageNumSize(request, () -> this.assetManaService.findAllList(assetMana, request));
    }
    @ApiOperation(value = "导出Excel")
    @RequestMapping(value = "assetinfo/excel",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo assetinfoExcel(AssetMana assetMana){
        try {
            List<AssetMana> list = this.assetManaService.findAllList(assetMana, null);
            return cc.mrbird.common.util.FileUtil.createExcelByPOIKit("资产信息表", list, AssetMana.class);
        } catch (Exception e) {
            log.error("导出资产信息表Excel失败", e);
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @ApiOperation(value = "导出csv")
    @RequestMapping(value = "assetinfo/csv",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo assetinfoCsv(AssetMana assetMana) {
        try {
            List<AssetMana> list = this.assetManaService.findAllList(assetMana, null);
            return cc.mrbird.common.util.FileUtil.createCsv("资产信息表", list, AssetMana.class);
        } catch (Exception e) {
            log.error("导出资产信息表Csv失败", e);
            return ResponseBo.error("导出Csv失败，请联系网站管理员！");
        }
    }
    @ApiOperation(value = "导入excel")
    @RequestMapping(value = "assetinfo/import",method = RequestMethod.POST)
    public ResponseBo eximport(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            a = assetManaService.batchImport(fileName, file);
            return  ResponseBo.ok(a);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseBo.error("导入数据失败,请重新检查excel数据导入");
    }

    @ApiOperation(value = "资产信息添加")
    @RequestMapping(value = "assetinfo/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo add(AssetMana assetMana,@RequestParam("fileName") MultipartFile file) {
        try {
            /*//可在单位ID后面+类型编码+序号
            assetMana.setAssetMana_id();*/
            User currentUser = super.getCurrentUser();
            boolean add = fileUtil.add(file, assetMana,currentUser);
            if(add){
                return ResponseBo.ok();
            }else {
                return ResponseBo.error("添加失败，请重新添加！");
            }

        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }


    @ApiOperation(value = "资产信息修改")
    @RequestMapping(value = "assetinfo/update",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(AssetMana assetMana,@RequestParam("fileName") MultipartFile file) {
        try {
            /*//可在单位ID后面+类型编码+序号
            assetMana.setAssetMana_id();*/
            boolean update = fileUtil.update(file, assetMana);
            if(update){
                return ResponseBo.ok();
            }else {
                return ResponseBo.error("修改失败，请重新修改！");
            }

        } catch (Exception e) {
            log.error("修改失败", e);
            return ResponseBo.error("修改失败，请重新修改！");
        }
    }

    @Log("删除资产信息")
    /*@RequiresPermissions("assetinfo:delete")*/
    @RequestMapping(value = "assetinfo/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.assetManaService.delete(ids);
            return ResponseBo.ok("删除资产信息成功！");
        } catch (Exception e) {
            log.error("删除资产信息失败", e);
            return ResponseBo.error("删除资产信息失败，请联系网站管理员！");
        }
    }
}
